Passed
Pull Request — development (#86)
by Vad
11:46
created

ShowMap.tsx ➔ ShowMap   C

Complexity

Conditions 10

Size

Total Lines 68
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10.0658

Importance

Changes 0
Metric Value
cc 10
eloc 58
dl 0
loc 68
ccs 21
cts 23
cp 0.913
crap 10.0658
rs 5.5744
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like ShowMap.tsx ➔ ShowMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import Map from '../../components/Map';
2
import { useParams } from "react-router-dom";
3
import axios, { AxiosError } from 'axios';
4
import { API_URL} from '../../helpers/config';
5
import { useEffect, useState, useRef } from 'react';
6
import { Zone } from '../../helpers/map/leaflet-types'
7
import { Scooter } from '../../helpers/bike-functions';
8
import AdminGate from '../../components/AdminGate';
9
10
import BikeList from '../../components/BikeList';
11
import RealTimeUpdate from '../../components/RealTimeUpdate';
12
13
export default function ShowMap() {
14 2
    const { city }  = useParams();
15 2
    const [zoneData, setZoneData] = useState<Zone[]>([]);
16 2
    const [scooterData, setScooterData] = useState<Scooter[]>([]);
17 2
    const [realTime, setRealTime] = useState(false);
18 2
    const timerRef = useRef<null | number>(null);
19 2
    const [trigger, setTrigger] = useState(0);
20
  
21
    
22 2
    useEffect(() => {
23 1
      const fetchScooters = async() => {
24 1
      try {
25 1
              const response = await axios.get(`${API_URL}/bike/city/${city}`);
26 1
              setScooterData(response.data);
27
          }
28
          catch(error)
29
          {
30
            const axiosError = error as AxiosError;
31
            console.log(axiosError?.response?.data);
32
          }
33
    }
34 1
    fetchScooters();
35
    },[city, trigger])
36
37 2
    useEffect(() => {
38 1
      const fetchZones = async () => {
39 1
      try {
40
41 1
              const response = await axios.get(`${API_URL}/zone/city/${city}`);
42 1
              setZoneData(response.data);
43
          }
44
          catch(error)
45
          {
46 1
            const axiosError = error as AxiosError;
47 1
            console.log(axiosError?.response?.data);
48
          }
49
    }
50 1
    fetchZones();
51
    },[city, trigger])
52
53
54 2
  return (
55
    <>
56
    <AdminGate/>
57
      <div data-testid="show-map" className="mx-auto sm:max-w-4xl"><Map city={city ?? "Göteborg"} zoneData={zoneData} scooterData={scooterData}/></div>
58
      <div className="flex flex-col items-center justify-center my-2 py-2 bg-red-100 rounded-md w-full sm:max-w-xl mx-auto">
59
      {/* <Label htmlFor="realtimetoggle">Vill du uppdatera kartan i realtid?</Label>
60
      <ToggleSwitch id="realtimetoggle" checked={realTime} onChange={updateRealTime}>Uppdatera i realtid?</ToggleSwitch> */}
61
      <RealTimeUpdate timerRef={timerRef} realTime={realTime} setRealTime={setRealTime} setTrigger={setTrigger}/>
62
      </div>
63
        <div id="scooter-list" className="p-4 flex flex-col justify-center w-full">
64
          <div className="mx-auto">
65
          <h2 className="text-4xl font-bold text-gray-900"> Cyklar i {city}: </h2>
66
        </div>
67
      {scooterData.length > 0 ? (
68
        <>
69
        <div className="mx-auto mb-5">
70
        <h2>Antal cyklar: <b>{scooterData.length}</b> </h2>
71
        </div>
72
        <BikeList scooterData={scooterData} isCityList={false}/>
73
        </>) : (
74
                <div className="mx-auto mb-5">
75
                    <p>Inga cyklar tillgängliga</p>
76
                </div>
77
    )}
78
    </div>
79
  </>
80
  )
81
};
82